package com.goodjob.musicplayer.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;



import com.goodjob.musicplayer.R;
import com.goodjob.musicplayer.entity.LoginBean;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText name;
    private EditText password;
    private Button btn_tijiao;
    private Button btn_zhuce;

    List<LoginBean> loginBeanList = new ArrayList<>();
    private CheckBox cb_jizhu;
    private CheckBox cb_zidong;

    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    private void initView() {
        sharedPreferences=getSharedPreferences("is",MODE_PRIVATE);
        editor=sharedPreferences.edit();
        name = (EditText) findViewById(R.id.name);
        password = (EditText) findViewById(R.id.password);
        btn_tijiao = (Button) findViewById(R.id.btn_tijiao);
        btn_zhuce = (Button) findViewById(R.id.btn_zhuce);

        btn_tijiao.setOnClickListener(this);
        btn_zhuce.setOnClickListener(this);
        LitePal.getDatabase();
        cb_jizhu = (CheckBox) findViewById(R.id.cb_jizhu);
        cb_jizhu.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b){
                    editor.putString("jizhu","jizhu");
                    editor.apply();
                }else {
                    editor.putString("jizhu","");
                    editor.apply();
                }
            }
        });
        cb_zidong = (CheckBox) findViewById(R.id.cb_zidong);
        cb_zidong.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b){
                    editor.putString("zidong","zidong");
                    editor.apply();
                }else {
                    editor.putString("zidong","");
                    editor.apply();
                }
            }
        });

        if (sharedPreferences.getString("jizhu","").equals("jizhu")){
            name.setText(sharedPreferences.getString("name",""));
            password.setText(sharedPreferences.getString("password",""));
            cb_jizhu.setChecked(true);
        }
        if (sharedPreferences.getString("zidong","").equals("zidong")){
            cb_zidong.setChecked(true);
            submit();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_tijiao:
                submit();
                break;
            case R.id.btn_zhuce:
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
                finish();
                break;
        }
    }



    private void submit() {
        // validate
        String nameString = name.getText().toString().trim();
        if (TextUtils.isEmpty(nameString)) {
            Toast.makeText(this, "姓名不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String passwordString = password.getText().toString().trim();
        if (TextUtils.isEmpty(passwordString)) {
            Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        if (nameString.equals("admin") && passwordString.equals("123")) {
            Toast.makeText(this, "输入正确", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(LoginActivity.this, ListActivity.class));
            finish();
            editor.putString("name",nameString);
            editor.putString("password",passwordString);
            editor.apply();
            return;
        }

        loginBeanList = LitePal.where("name=? and password=?", nameString, passwordString).find(LoginBean.class);
        if (loginBeanList.size() > 0) {
            Toast.makeText(this, "输入正确", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(LoginActivity.this, ListActivity.class));
            finish();
        } else {
            Toast.makeText(this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
        }

        editor.putString("name",nameString);
        editor.putString("password",passwordString);
        editor.apply();
        finish();
        // TODO validate success, do something


    }
}
